<template>
  <div :style="'color: #1890ff;height:' + heightStyle">
    <em v-if="iconClose" class="el-icon-refresh-right" style="float: left;line-height: 2.3;margin-right: 5px;" />
    <single
      ref="singleRef"
      :upload-style="uploadStyle"
      :data-type="dataType"
      :upload-btn-name="uploadBtnName"
      @appendData="appendData"
      @okPostUploadHook="okPostUploadHook"
    />
  </div>
</template>
<script>
import { reUploadHistory } from '@/api/file/onlineEditing/fileEditHistory.js'
import Single from '@/components/File/Upload/single'
export default {
  components: {
    Single
  },
  props: {
    dataType: {
      type: String,
      default: () => ''
    },
    uploadBtnName: {
      type: String,
      default: () => '上传'
    },
    heightStyle: {
      type: String,
      default: () => '30px'
    },
    getFileObject: {
      type: Object,
      default: () => {}
    },
    iconClose: {
      type: Boolean,
      default: () => true
    },
    uploadStyle: {
      type: String,
      default: () => 'margin-left: 2px;border: none;padding: 0px 0px 0px 18px;color: rgb(24, 144, 255);font-size: 14px;font-weight: 350;transform: translateY(-170%);'
    }
  },
  data() {
    return {
    }
  },
  watch: {
    getFileObject(val) {
      this.getFileObject = val
    }
  },
  created() {
  },
  methods: {
    /**
     * 先把旧文件 放到文件历史中.
     * 新文件的 url, name, real_name, md5, suffix, sizes 字段
     * 放到旧文件, ,删掉新文件
     * @param data
     */
    appendData(data) {
      const newId = data.fileId || data.id
      const oldId = this.getFileObject.id
      console.log('newId', newId, 'oldId', oldId)
      if (oldId === undefined || oldId === null || oldId === '') {
        data.directoryId = this.getFileObject.directoryId
        data.directoryName = this.getFileObject.directoryName
        data.businessId = this.getFileObject.businessId
        data.originalName = this.getFileObject.name
        data.name = data.fileName || data.name
        data.id = data.fileId || data.id
        this.$emit('replaceNewFileNotOldId', data)
      } else {
        reUploadHistory(newId, oldId).then(response => {
          console.log(response)
          if (response.code === 200 && response.data !== null) {
            var newFile = response.data
            // 重置文件类别
            newFile.directoryId = this.getFileObject.directoryId
            newFile.directoryName = this.getFileObject.directoryName
            this.$emit('replaceNewFile', newFile)
          } else {
            this.$emit('replaceNewFile', null)
          }
        })
      }
    },
    // windows上传窗口点击确定后执行以下代码
    okPostUploadHook() {
      this.$emit('okPostUploadHook', '')
    }
  }
}
</script>
<style scoped>
</style>
